﻿using KINLO.UI.Main.MES;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;


namespace KINLO.UI.Main.Hepler
{
    public class NPOIHandler
    {
        public List<AlarmInfo> ParseExcel(string fileName)
        {
            try
            {
                List<AlarmInfo> alarmInfos = new List<AlarmInfo>();
                // 获取当前目录
                string currentDirectory = Directory.GetCurrentDirectory();

                // 文件夹名
                string folderName = "AlarmInfoConfig";

                // 拼接文件路径
                string filePath = Path.Combine(currentDirectory, folderName, fileName);

                using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    //IWorkbook workbook = new XSSFWorkbook(file);

                    HSSFWorkbook workbook = new HSSFWorkbook(file);
                    ISheet sheet = workbook.GetSheetAt(0); // 假设数据在第一个工作表中

                    int i = 1;
                    // 假设列标题为：类别、设备类型 (读取地址)、地址 (读取地址)、内容、报警条件
                    int categoryIndex = -1;
                    int deviceTypeIndex = -1;
                    int alarmAddressIndex = -1;
                    int alarmContentIndex = -1;
                    int alarmconditionIndex = -1;


                    // 获取第二行作为列标题行
                    IRow headerRow = sheet.GetRow(1);
                    if (headerRow != null)
                    {
                        for (int cellIndex = 0; cellIndex < headerRow.LastCellNum; cellIndex++)
                        {
                            var cellValue = headerRow.GetCell(cellIndex)?.StringCellValue;

                            // 根据列标题找到对应的索引
                            switch (cellValue)
                            {
                                case "类别":
                                    categoryIndex = cellIndex;
                                    break;
                                case "设备类型 (读取地址)":
                                    deviceTypeIndex = cellIndex;
                                    break;
                                case "地址 (读取地址)":
                                    alarmAddressIndex = cellIndex;
                                    break;
                                case "内容":
                                    alarmContentIndex = cellIndex;
                                    break;
                                case "触发条件":
                                    alarmconditionIndex = cellIndex;
                                    break;
                            }
                        }
                    }

                    // 如果所有索引都找到了，则进行数据读取
                    if (categoryIndex >= 0 && deviceTypeIndex >= 0 && alarmAddressIndex >= 0 && alarmContentIndex >= 0 && alarmconditionIndex >= 0)
                    {
                        for (int rowIndex = 1; rowIndex <= sheet.LastRowNum; rowIndex++)
                        {
                            IRow row = sheet.GetRow(rowIndex);

                            // 根据索引读取对应的数据
                            string category = row.GetCell(categoryIndex)?.StringCellValue;
                            string deviceType = row.GetCell(deviceTypeIndex)?.StringCellValue;
                            string alarmAddress = row.GetCell(alarmAddressIndex)?.StringCellValue;
                            string alarmContent = row.GetCell(alarmContentIndex)?.StringCellValue;
                            string alarmcondition = row.GetCell(alarmconditionIndex)?.StringCellValue;
                            if (category != null && category.Contains("0"))
                            {
                                alarmInfos.Add(new AlarmInfo
                                {
                                    Id = i++,
                                    DeviceType = deviceType,
                                    AlarmAddress = deviceType.Replace("_Bit", "") + alarmAddress,
                                    AlarmContent = alarmContent,
                                    Alarmcondition = alarmcondition.Contains("1") ? 1 : 0,
                                });
                            }
                        }
                    }
                }
                return alarmInfos;
            }
            catch (Exception ex)
            {
                // 在这里处理异常，您可以使用消息框或记录到日志文件中
                System.Windows.MessageBox.Show("加载报警信息时发生错误：" + ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);

                return null;
            }


        }
    }
}
